package bookez.view.bean;


import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import bookez.model.service.BusTypeService;
import bookez.model.service.RouteService;
import bookez.model.service.TripDailyRecordService;
import bookez.model.service.TripScheduleService;
import bookez.model.service.UserService;
import bookez.view.servicelocator.ServiceLocator;
import bookez.view.util.FacesUtil;

@ManagedBean
@ApplicationScoped
public class ServiceLocatorBean implements ServiceLocator, Serializable {
	
	public ServiceLocatorBean() {
		
	}//end default constructor
	
	@PostConstruct
	public void init() {
		iniAppContext();
		iniServices();
	}//end method init

	private void iniAppContext() {
		ServletContext servletContext = FacesUtil.getServletContext();
		this.appContext = WebApplicationContextUtils.getRequiredWebApplicationContext( servletContext );		
	}//end method getAppContext
	
	private void iniServices() {
		this.userService = (UserService) lookupService(USER_SERVICE);
		this.routeService = (RouteService) lookupService(ROUTE_SERVICE);
		this.busTypeService = (BusTypeService) lookupService(BUSTYPE_SERVICE);
		this.tripScheduleService = (TripScheduleService) lookupService(TRIP_SCHEDULE_SERVICE);
		this.tripDailyRecordService = (TripDailyRecordService) lookupService( TRIP_DAILY_RECORD_SERVICE );
	}//end method iniServices
	
	private Object lookupService( String serviceBeanName ) {		
		return appContext.getBean( serviceBeanName );		
	}//end method lookupService
	
	
	@Override
	public UserService getUserService() { return this.userService; }

	@Override
	public RouteService getRouteService() { return this.routeService; }

	@Override
	public BusTypeService getBusTypeService() {	return this.busTypeService; }
	
	@Override
	public TripScheduleService getTripScheduleService() { return tripScheduleService; }
	
	@Override
	public TripDailyRecordService getTripDailyRecordService() {	return tripDailyRecordService; }
	
	/*----------------------------
	 * Instance Variables
	 ----------------------------*/
	// The Spring Application Context
	private ApplicationContext appContext;
	
	// Services Handler
	private UserService userService;
	private RouteService routeService;
	private BusTypeService busTypeService;
	private TripScheduleService tripScheduleService;
	private TripDailyRecordService tripDailyRecordService;
	
	/*-----------------------------
	 * All Services Names
	 -----------------------------*/
	private static final String USER_SERVICE 		= "userService";
	private static final String ROUTE_SERVICE 		= "routeService";
	private static final String BUSTYPE_SERVICE		= "busTypeService";
	private static final String TRIP_SCHEDULE_SERVICE = "tripScheduleService";
	private static final String TRIP_DAILY_RECORD_SERVICE = "tripDailyRecordService";

	private static final long serialVersionUID = 3147726035886750191L;
}//end class ServiceLocatorBean
